﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Text.RegularExpressions;

namespace MangaManagerLib
{
    public class WebWorker
    {
        private HttpWebRequest httpReq;

        public HttpWebRequest HttpReq
        {
            get { return httpReq; }
            set { httpReq = value; }
        }
        public static string RemoveHTMLTag(string s)
        {
            string result;
            string htmlregex = @"<([/\s\d\w&?%:~`,;\^\%\\'\.""=+-–])[^>]*>";
            s.Replace("\r", "").Replace("\n", "").Replace("\t", "").TrimStart().TrimEnd();
            MatchCollection matches = Regex.Matches(s, htmlregex, RegexOptions.IgnoreCase);
            foreach (Match m in matches)
            {
                s = s.Replace(m.Value, string.Empty);
            }
            result = s;
            return result;
        }
        public static string GetContent(string url)
        {
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);            
            req.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
            req.UserAgent = "Mozilla/5.0";
            req.Method = "GET";
            HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
            if (resp.StatusCode != HttpStatusCode.OK)
                return null;
            StreamReader reader = new StreamReader(resp.GetResponseStream() );
            string sourceCode = reader.ReadToEnd();
            sourceCode = System.Web.HttpUtility.HtmlDecode(sourceCode);
            reader.Close();
            resp.Close();           
            return sourceCode;
        }
        public static String GetURLHost(string url)
        {
            //startIndex can begin from http://(7) or https:// (8)
            int endHostUrlIndex = url.IndexOf("/", 9);
            string hostURL = url.Substring(0, endHostUrlIndex);
            return hostURL;
        }
        
        public WebWorker(String url)
        {
            httpReq = (HttpWebRequest)WebRequest.Create(url);
            httpReq.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
            httpReq.UserAgent = "Mozilla/5.0";
            httpReq.Method = "GET";
        }

        public void AddCookie(string name, string value, string domain)
        {
           
            if (httpReq.CookieContainer == null)
                httpReq.CookieContainer = new CookieContainer();
            httpReq.CookieContainer.Add(new Uri(domain), new Cookie(name, value));
        }

        public String GetResponeContent()
        {
            String resultString = String.Empty;
            HttpWebResponse resp = (HttpWebResponse)httpReq.GetResponse();
            if (resp.StatusCode != HttpStatusCode.OK)
                return null;
            StreamReader reader = new StreamReader(resp.GetResponseStream());
            resultString = reader.ReadToEnd();
            resultString = System.Web.HttpUtility.HtmlDecode(resultString);
            reader.Close();
            resp.Close();
            return resultString;
        }
        public static string ExecutePostRequest(string url, Dictionary<String, String> collection)
        {
            string result = string.Empty;
            HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url);
            myRequest.Method = "POST";
            myRequest.AllowAutoRedirect = true;
            myRequest.ContentType = "application/x-www-form-urlencoded";
            myRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.1; rv:13.0) Gecko/20100101 Firefox/13.0.1";
            byte[] byteArray = Encoding.ASCII.GetBytes(collection.ToQueryString());
            myRequest.ContentLength = byteArray.Length;
            Stream dataStream = myRequest.GetRequestStream();
            dataStream.Write(byteArray, 0, byteArray.Length);
            
            using (StreamReader streamIn =
                           new StreamReader(myRequest.GetResponse().GetResponseStream()))
            {
                result = streamIn.ReadToEnd();
            }
            return result;
        }
    }
}
